//
//  EHPopverAnimator.m
//  SYBaseProject
//
//  Created by SY on 2025/6/19.
//  Copyright © 2025 YYB. All rights reserved.
//

#import "EHPopoverAnimator.h"
#import "EHPresentationController.h"
@interface EHPopoverAnimator ()

@end
@implementation EHPopoverAnimator

+ (instancetype)animatorWithCompletion:(EHPopoverAnimatorCompletion)completion {
    EHPopoverAnimator *animator = [[self alloc] init];
    animator.completion = completion;
    return animator;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _presentedFrame = [UIScreen mainScreen].bounds;
    }
    return self;
}

- (void)presentFromViewController:(UIViewController *)fromViewController
               toViewController:(UIViewController *)toViewController
                 isTransform3D:(BOOL)isTransform3D {
    
    self.isTransform3D = isTransform3D;
    toViewController.modalPresentationStyle = UIModalPresentationCustom;
    toViewController.transitioningDelegate = self;
    
    [fromViewController presentViewController:toViewController animated:YES completion:nil];
}

#pragma mark - UIViewControllerTransitioningDelegate

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented
                                                      presentingViewController:(UIViewController *)presenting
                                                          sourceViewController:(UIViewController *)source {
    EHPresentationController *pc = [[EHPresentationController alloc]
                                   initWithPresentedViewController:presented
                                   presentingViewController:presenting];
    pc.presentedFrame = self.presentedFrame;
    return pc;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                 presentingController:(UIViewController *)presenting
                                                                     sourceController:(UIViewController *)source {
    self.isPresented = YES;
    if (self.completion) self.completion(YES);
    return self;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.isPresented = NO;
    if (self.completion) self.completion(NO);
    return self;
}

#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.5;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (self.isPresented) {
        [self animatePresentationWithContext:transitionContext];
    } else {
        [self animateDismissalWithContext:transitionContext];
    }
}

- (void)animatePresentationWithContext:(id<UIViewControllerContextTransitioning>)context {
    UIView *toView = [context viewForKey:UITransitionContextToViewKey];
    UIView *containerView = context.containerView;
    
    toView.frame = [context finalFrameForViewController:[context viewControllerForKey:UITransitionContextToViewControllerKey]];
    [containerView addSubview:toView];
    
    if (self.isTransform3D) {
        toView.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1.0);
        toView.alpha = 0.0;
        
        [UIView animateWithDuration:[self transitionDuration:context]
                         animations:^{
            toView.layer.transform = CATransform3DIdentity;
            toView.alpha = 1.0;
        } completion:^(BOOL finished) {
            [context completeTransition:YES];
        }];
    } else {
        CGRect finalFrame = toView.frame;
        toView.frame = CGRectOffset(finalFrame, 0, CGRectGetHeight(finalFrame));
        
        [UIView animateWithDuration:[self transitionDuration:context]
                         animations:^{
            toView.frame = finalFrame;
        } completion:^(BOOL finished) {
            [context completeTransition:YES];
        }];
    }
}

- (void)animateDismissalWithContext:(id<UIViewControllerContextTransitioning>)context {
    UIView *fromView = [context viewForKey:UITransitionContextFromViewKey];
    
    if (self.isTransform3D) {
        [UIView animateWithDuration:[self transitionDuration:context]
                         animations:^{
            fromView.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1.0);
            fromView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [fromView removeFromSuperview];
            [context completeTransition:YES];
        }];
    } else {
        CGRect finalFrame = fromView.frame;
        
        [UIView animateWithDuration:[self transitionDuration:context]
                         animations:^{
            fromView.frame = CGRectOffset(finalFrame, 0, CGRectGetHeight(finalFrame));
        } completion:^(BOOL finished) {
            [fromView removeFromSuperview];
            [context completeTransition:YES];
        }];
    }
}
@end

